import React ,{Suspense} from 'react';
import { Switch, Route, Redirect } from 'react-router-dom';

const RouterView = function RouterView(props) {
    let { routes } = props;
    return <Switch>
        {routes.map((route, index) => {
            let { redirect, from, to, exact, path, name, component: Component, meta } = route,
                config = {};
            if (redirect) {
                config = { to };
                if (from) config.from = from;
                if (exact) config.exact = true;
                return <Redirect key={index} {...config} />
            }
            config = { path };
            if (exact) config.exact = true;
            return <Route key={index} {...config} render={() => {
                // 做一些特殊的处理，例如：登录态检验、导航守卫等
                return <Suspense fallback={'加载中'}>
                    <Component />;
                </Suspense>
            }} />;
        })}
    </Switch>;
};
export default RouterView;
